home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / lowlevel / VBlankInt / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-17  |  7.4 KB  |  352 lines

  1. /****** CDGS/VBlankInt **********************************************************
  2. *
  3. *   NAME
  4. *       VBlankInt -- test program for lowlevel.library/AddVBlankInt(),
  5. *                and lowlevel.library/RemVBlankInt().
  6. *
  7. *   SYNOPSIS
  8. *       VBlankInt
  9. *
  10. *   FUNCTION
  11. *
  12. *       Test program for lowlevel.library/AddVBlankInt() and
  13. *       lowlevel.library/RemVBlankInt().
  14. *
  15. *       The program automatically terminates after one second.
  16. *
  17. *   INPUTS
  18. *       None
  19. *
  20. *   RESULT
  21. *       RETURN_OK (0)       -   success
  22. *       RETURN_WARN (5)     -   warning
  23. *       RETURN_ERROR (10)   -   error
  24. *       RETURN_FAIL (20)    -   failure
  25. *
  26. *   EXAMPLE
  27. *
  28. *   NOTES
  29. *
  30. *   BUGS
  31. *       Hopefully very few.
  32. *
  33. *   SEE ALSO
  34. *
  35. ******************************************************************************
  36. *
  37. */
  38.  
  39. /*
  40.  * System includes
  41.  */
  42.  
  43. #include <exec/types.h>
  44. #include <exec/memory.h>
  45.  
  46. #include <dos/dos.h>
  47.  
  48. #include <graphics/gfx.h>
  49. #include <graphics/gfxbase.h>
  50. #include <graphics/gfxmacros.h>
  51.  
  52. #include <intuition/intuition.h>
  53. #include <intuition/screens.h>
  54.  
  55. #include <devices/timer.h>
  56.  
  57. #include <libraries/lowlevel.h>
  58.  
  59. #include <clib/exec_protos.h>
  60. #include <clib/dos_protos.h>
  61. #include <clib/graphics_protos.h>
  62. #include <clib/intuition_protos.h>
  63. #include <clib/lowlevel_protos.h>
  64. #include <clib/alib_protos.h>
  65. /*#include <clib/debug_protos.h>*/
  66. extern void kprintf(const char *,...);
  67.  
  68. #include <pragmas/lowlevel_pragmas.h>
  69.  
  70. /*
  71.  * ANSI includes
  72.  */
  73.  
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. #include <string.h>
  77.  
  78. /*
  79.  * Local includes
  80.  */
  81.  
  82. #define MAIN
  83.  
  84. #include "vblankint.h"
  85.  
  86. /****** VBlankInt/main ******************************************
  87. *
  88. *   NAME
  89. *       main -- main entry point
  90. *
  91. *   SYNOPSIS
  92. *       main(argc,argv);
  93. *
  94. *       void main(int argc,char *argv[]);
  95. *
  96. *   FUNCTION
  97. *       Main entry point.
  98. *
  99. *   INPUTS
  100. *       argc -- argument count
  101. *       argv -- argument value arrraay
  102. *
  103. *   RESULT
  104. *       None
  105. *
  106. *   EXAMPLE
  107. *
  108. *   NOTES
  109. *
  110. *   BUGS
  111. *       If there are any, you can get to 'em from here!
  112. *
  113. *   SEE ALSO
  114. *       shutdown()
  115. *
  116. ******************************************************************************
  117. *
  118. */
  119. void main(int argc,char *argv[])
  120. {
  121.  
  122.     BYTE error;
  123.  
  124.     /*
  125.      * Open libraries
  126.      */
  127.  
  128.     /* Open graphics.library */
  129.     GfxBase=(struct GfxBase *) OpenLibrary("graphics.library",
  130.         KICKSTART_VERSION);
  131.     if (!GfxBase) {
  132.         Printf("%s: Error opening graphics.library V%ld\n",
  133.             PROGRAM_NAME,KICKSTART_VERSION);
  134.         goodbye(RETURN_FAIL);
  135.     }
  136.  
  137.     /* Open intuition.library */
  138.     IntuitionBase=OpenLibrary("intuition.library",KICKSTART_VERSION);
  139.     if (!IntuitionBase) {
  140.         Printf("%s: Error opening intuition.library V%ld\n",
  141.             PROGRAM_NAME,KICKSTART_VERSION);
  142.         goodbye(RETURN_FAIL);
  143.     }
  144.  
  145.     /* Open lowlevel.library */
  146.     LowLevelBase=OpenLibrary("lowlevel.library",KICKSTART_VERSION);
  147.     if (!LowLevelBase) {
  148.         Printf("%s: Error opening lowlevel.library V%d\n",
  149.             PROGRAM_NAME,KICKSTART_VERSION);
  150.         goodbye(RETURN_FAIL);
  151.     }
  152.  
  153.     /*
  154.      * Open devices
  155.      */
  156.  
  157.     /* Create timer.device reply port */
  158.     timerPort=CreateMsgPort();
  159.     if (!timerPort) {
  160.         Printf("%s: Error creating timer.device reply port\n",PROGRAM_NAME);
  161.         goodbye(RETURN_FAIL);
  162.     }
  163.  
  164.     /* Create timer.device I/O request */
  165.     timerRequest=CreateIORequest(timerPort,sizeof(struct timerequest));
  166.     if (!timerRequest) {
  167.         Printf("%s: Error creating timer.device I/O request\n",PROGRAM_NAME);
  168.         goodbye(RETURN_FAIL);
  169.     }
  170.  
  171.     /* Open timer.device microhertz timer */
  172.     error=OpenDevice("timer.device",UNIT_MICROHZ,timerRequest,NULL);
  173.     if (error) {
  174.         Printf("%s: Error %ld opening timer.device\n",PROGRAM_NAME,
  175.             (LONG) error);
  176.         goodbye(RETURN_FAIL);
  177.     }
  178.     timerOpen=TRUE;
  179.  
  180.     /*
  181.      * Open test environment
  182.      */
  183.  
  184.     /* Open high-resolution non-interlaced screen in default mode */
  185.     screen=OpenScreenTags(NULL,
  186.         SA_Width, SCREEN_WIDTH,
  187.         SA_Height, SCREEN_HEIGHT,
  188.         SA_Depth, SCREEN_DEPTH,
  189.         SA_DisplayID, SCREEN_DISPLAYID,
  190.         SA_Title, PROGRAM_NAME,
  191.         TAG_DONE);
  192.     if (!screen) {
  193.         Printf("%s: Error opening screen\n",PROGRAM_NAME);
  194.         goodbye(RETURN_FAIL);
  195.     }
  196.  
  197.     /* Compute vertical blank beam position threshold */
  198.     vbeamThreshold=(GfxBase->DisplayFlags&NTSC)?
  199.         VBEAMPOS_THRESHOLD_NTSC:VBEAMPOS_THRESHOLD_PAL;
  200.  
  201.     /*
  202.  
  203.     /*
  204.      * Test vertical blank interrupt handler
  205.      */
  206.  
  207.     /* Set-up timer.device I/O request to wait one second */
  208.     timerRequest->tr_node.io_Command=TR_ADDREQUEST;
  209.     timerRequest->tr_time.tv_secs=1;
  210.     timerRequest->tr_time.tv_micro=0;
  211.  
  212.     /* Add vertical blank interrupt handler */
  213.     vblankIntHandle=AddVBlankInt(vblankInterrupt,(APTR) VBLANKINT_COOKIE);
  214.     if (!vblankIntHandle) {
  215.         Printf("%s: Error adding vertical blank interrupt handler\n",PROGRAM_NAME);
  216.         goodbye(RETURN_FAIL);
  217.     }
  218.  
  219.     /* Wait one second */
  220.     error=DoIO(timerRequest);
  221.  
  222.     /* Remove vertical blank interrupt handler */
  223.     RemVBlankInt(vblankIntHandle);
  224.  
  225.     /* Check for error in timer.device I/O ... */
  226.     if (error) {
  227.         Printf("%s: Error %ld waiting with timer.device\n",PROGRAM_NAME,
  228.             (LONG) error);
  229.         goodbye(RETURN_FAIL);
  230.     }
  231.  
  232.     /*
  233.      * Diagnostics
  234.      */
  235.  
  236.     /* Output total calls to vertical blank interrupt handler */
  237.     printf("%s: %lu total calls to vertical blank interrupt handler\n",
  238.         PROGRAM_NAME,vblankIntCall);
  239.  
  240.     /* If any bad data to vertical blank interrupt handler ... */
  241.     if (vblankIntBadData) {
  242.         /* Warning */
  243.         Printf("%s: %lu calls to vertical blank interrupt handler with bad data\n",
  244.             PROGRAM_NAME,vblankIntBadData);
  245.     }
  246.  
  247.     /* If any out of thresholdcalls to vertical blank interrupt handler ... */
  248.     if (vblankIntThreshold) {
  249.         /* Warning */
  250.         Printf("%s: %lu calls to vertical blank interrupt handler with vertical beam position >%ld\n",
  251.             PROGRAM_NAME,vblankIntThreshold,vbeamThreshold);
  252.     }
  253.  
  254.     /*
  255.      * Termination
  256.      */
  257.  
  258.     /* Exit */
  259.     goodbye( (vblankIntBadData || vblankIntThreshold) ?RETURN_WARN:RETURN_OK);
  260.  
  261. }
  262.  
  263. /****** VBlankInt/goodbye ******************************************
  264. *
  265. *   NAME
  266. *       goodbye -- terminate program
  267. *
  268. *   SYNOPSIS
  269. *       goodbye(returnCode);
  270. *
  271. *       void goodbye(int returnCode);
  272. *
  273. *   FUNCTION
  274. *       Terminate program.
  275. *
  276. *   INPUTS
  277. *       returnCode -- return code (from dos/dos.h RETURN_*)
  278. *
  279. *   RESULT
  280. *       None
  281. *
  282. *   EXAMPLE
  283. *
  284. *   NOTES
  285. *
  286. *   BUGS
  287. *
  288. *   SEE ALSO
  289. *       main()
  290. *
  291. ******************************************************************************
  292. *
  293. */
  294. void goodbye(int returnCode)
  295. {
  296.  
  297.     /*
  298.      * Close test environment
  299.      */
  300.  
  301.     /* Close screen */
  302.     if (screen) {
  303.         CloseScreen(screen);
  304.     }
  305.  
  306.     /*
  307.      * Close devices
  308.      */
  309.  
  310.     /* Close timer.device */
  311.     if (timerOpen) {
  312.         CloseDevice(timerRequest);
  313.     }
  314.  
  315.     /* Destroy timer.device I/O request */
  316.     if (timerRequest) {
  317.         DeleteIORequest(timerRequest);
  318.     }
  319.  
  320.     /* Destroy timer.device reply port */
  321.     if (timerPort) {
  322.         DeleteMsgPort(timerPort);
  323.     }
  324.  
  325.     /*
  326.      * Close libraries
  327.      */
  328.  
  329.     /* Close lowlevel.library */
  330.     if (LowLevelBase) {
  331.         CloseLibrary(LowLevelBase);
  332.     }
  333.  
  334.     /* Close intuition.library */
  335.     if (IntuitionBase) {
  336.         CloseLibrary(IntuitionBase);
  337.     }
  338.  
  339.     /* Close graphics.library */
  340.     if (GfxBase) {
  341.         CloseLibrary(GfxBase);
  342.     }
  343.  
  344.     /*
  345.      * Exit
  346.      */
  347.  
  348.     /* Exit */
  349.     exit(returnCode);
  350.  
  351. }
  352.